package com.yjy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.comm.util.BaseUtils;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysUser;
import com.yjy.service.SysDeptService;
import com.yjy.service.SysRoleService;
import com.yjy.service.SysUserService;

@Controller
@RequestMapping("user")
public class SysUserController{
	
	@Autowired
	private SysUserService userService;
	
	@Autowired
	private SysDeptService deptService;
	
	@Autowired
	private SysRoleService roleService;
	
	@RequestMapping("list")
	public String list(Model model,HttpServletRequest request,HttpSession session){
		Map<?, ?> dMap=new HashMap<>();
		Map<String, Object> uMap=init(request);
		List<SysUser> list=userService.listAll(uMap);
		for (SysUser user : list) {
			String pass=BaseUtils.decrypt(user.getPassword(), BaseUtils.PASSWORD_KEY);
			user.setPassword(pass);
		}
		model.addAttribute("userList", list);			
		model.addAttribute("deptList", deptService.listAll(dMap));
		SysUser user=(SysUser) session.getAttribute("user");		
		List<SysMenu> power=roleService.selectPower(user.getUserId());
		String menus="";
		for (SysMenu menu : power) {
			menus+=menu.getUrl()+",";
		}
		model.addAttribute("power", menus);
		return "sys/sysuser";
	}
	
	private Map<String, Object> init(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		String account=request.getParameter("acc");
		if (account!=null) {
			map.put("account", account);
		}
		String username=request.getParameter("uname");
		if (username!=null) {
			map.put("username", username);
		}
		String sex=request.getParameter("csex");
		if (sex!=null) {
			map.put("sex", sex);
		}
		String status=request.getParameter("cstate");
		if (status!=null) {
			map.put("status", status);
		}
		String mobile=request.getParameter("cmobile");
		if (mobile!=null) {
			map.put("mobile", mobile);
		}
		String email=request.getParameter("cemail");
		if (email!=null) {
			map.put("email", email);
		}
		String startTime=request.getParameter("startTime");
		if (startTime!=null) {
			map.put("startTime", startTime);
		}
		String endTime=request.getParameter("endTime");
		if (endTime!=null) {
			map.put("endTime", endTime);
		}
		return map;
	}
	
	@RequestMapping("save")
	public String save(SysUser user,HttpSession session){
		SysUser sysUser=(SysUser) session.getAttribute("user");
		user.setCreateUserId(sysUser.getUserId());
		userService.save(user);
		return "redirect:/user/list";
	}
	
	@RequestMapping("delete")
	public String delete(String userId){
		userService.delete(userId);
		return "redirect:/user/list";
	}
	
	@RequestMapping("move")
	public String move(String userId,String deptId){
		userService.move(userId, deptId);
		return "redirect:/user/list";
	}
	
	@RequestMapping("queryUserIds")
	public String queryUserIds(String ids,Model model){
		List<SysUser>  users=userService.queryUserIds(ids);
		model.addAttribute("userList", users);
		return "sys/sysuser";
	}
	
	@RequestMapping(value="checkName",produces="application/json;charset=utf-8")
	@ResponseBody
	public String checkName(String account){
		boolean state=userService.checkName(account);
		if (state) {
			return "ok";
		}
		return "error";
	}
	
	@RequestMapping("update")
	public String update(SysUser user){
		userService.update(user);
		return "redirect:/user/list";
	}
}
